﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ASPItalia.ModelVirtualCasting.Silverlight.ViewModels;
using MVCSS = ASPItalia.ModelVirtualCasting.Silverlight.ModelVirtualCastingServices;

namespace ASPItalia.ModelVirtualCasting.Silverlight
{
  public partial class MainPage : UserControl
  {
    public static readonly DependencyProperty IsMovedProperty = DependencyProperty.RegisterAttached(
      "IsMoved",
      typeof(Boolean),
      typeof(MainPage),
      new PropertyMetadata(false)
      );

    public static void SetIsMoved(UIElement element, Boolean value)
    {
      element.SetValue(IsMovedProperty, value);
      //registrazione eventi
    }

    public static Boolean GetIsMoved(UIElement element)
    {
      return (Boolean)element.GetValue(IsMovedProperty);
    }
    
    public MainViewModel ViewModel { get; private set; }

    Dictionary<FrameworkElement, double> elementoriginalPosition = new Dictionary<FrameworkElement, double>();
    public MainPage()
    {
      InitializeComponent();
      ViewModel = new MainViewModel();
      this.DataContext = ViewModel;
    }

    private void modelListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
      if (e.RemovedItems.Count == 1)
      {
        foreach (object item in modelListBox.Items)
        {
          FrameworkElement element = modelListBox.ItemContainerGenerator.ContainerFromItem(item) as FrameworkElement;

          if (MainPage.GetIsMoved(element))
          {
            ((PlaneProjection)element.Projection).GlobalOffsetX = elementoriginalPosition[element];
            MainPage.SetIsMoved(element, false);
          }
        }
      }
      if (e.AddedItems.Count == 1)
      {
        FrameworkElement element = (FrameworkElement)modelListBox.ItemContainerGenerator.ContainerFromItem(e.AddedItems[0]);
        if (element != null)
        {
          //Storyboard selectionStoryBoard = new Storyboard();
          PlaneProjection projection = ((PlaneProjection)element.Projection);
          //DoubleAnimation xOfAnimation = new DoubleAnimation { Duration = new Duration(TimeSpan.FromMilliseconds(300)), To = 460 };
          //Storyboard.SetTarget(xOfAnimation, projection);
          //Storyboard.SetTargetProperty(xOfAnimation, new PropertyPath(PlaneProjection.GlobalOffsetXProperty));
          //selectionStoryBoard.Children.Add(xOfAnimation);                   
          if (!elementoriginalPosition.ContainsKey(element))
          {
            elementoriginalPosition.Add(element, projection.GlobalOffsetX);
          }
          projection.GlobalOffsetX = 490;
          MainPage.SetIsMoved(element, true);
          //selectionStoryBoard.Begin();

          this.ViewModel.CurrentModel = element.DataContext as MVCSS.Model;

          //ChildWindow _newWin = new ModelDetails(this.ViewModel);
          //_newWin.Show();
        }
      }
    }

    private void modelImage_ImageFailed(object sender, ExceptionRoutedEventArgs e)
    {
      Exception err = e.ErrorException;
    }

    private void OnMouseRightButtonDown(object sender, MouseButtonEventArgs e)
    {
      RightMenu.HorizontalOffset = e.GetPosition(null).X + 2;
      RightMenu.VerticalOffset = e.GetPosition(null).Y + 2;
      RightMenu.IsOpen = true;
      e.Handled = true;
    }

    private void LayoutRoot_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
      RightMenu.IsOpen = false;
    }
  }
}
